package com.wooming.studentinfosystem.service.impl;

import com.wooming.studentinfosystem.dto.StudentInfoDTO;
import com.wooming.studentinfosystem.mapper.StudentUpdateMapper;
import com.wooming.studentinfosystem.service.interf.StudentUpdate;
import com.wooming.studentinfosystem.util.MapAndJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 学生更新服务实现类
 *
 * @author Wooming
 */
@Service
public class StudentUpdateService implements StudentUpdate {

    @Autowired
    private StudentUpdateMapper studentUpdateMapper;

    @Override
    public void updateStudent(String studentId, StudentInfoDTO studentInfoDTO) {
        //构建动态SQL参数
        Map<String, Object> params = new HashMap<>();
        params.put("originalStudentId", studentId);
        params.put("studentId", studentInfoDTO.getStudentId());
        params.put("name", studentInfoDTO.getName());
        params.put("age", studentInfoDTO.getAge());
        params.put("gender", studentInfoDTO.getGender());
        params.put("classNum", studentInfoDTO.getClassNum());
        params.put("type", studentInfoDTO.getType());
        params.put("province", studentInfoDTO.getProvince());
        params.put("city", studentInfoDTO.getCity());
        params.put("street", studentInfoDTO.getStreet());
        params.put("doorNumber", studentInfoDTO.getDoorNumber());
        params.put("major", studentInfoDTO.getMajor());
        params.put("supervisor", studentInfoDTO.getSupervisor());
        params.put("researchField", studentInfoDTO.getResearchFiled());
        params.put("scores", MapAndJson.mapToJson(studentInfoDTO.getScores()));

        studentUpdateMapper.updateStudent(params);
    }

    @Override
    public boolean isStudentIdExists(String studentId) {
        return studentUpdateMapper.countByStudentId(studentId) > 0;
    }
}
